package com.mall.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.ums.AdminRoleRelation;
import com.mall.model.domain.ums.Role;
import com.mall.ums.mapper.RoleMapper;
import com.mall.ums.service.AdminRoleRelationService;
import com.mall.ums.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author clutchcitylee
 * @create 2023-03-12-16:13
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    AdminRoleRelationService adminRoleRelationService;

    @Override
    public List<Role> getRoleByAdminId(Integer adminId) {
        LambdaQueryWrapper<AdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRoleRelation::getAdminId, adminId);
        List<AdminRoleRelation> adminRoleRelations = adminRoleRelationService.list(queryWrapper);
        List<Long> roleIds = adminRoleRelations.stream().map(item -> {
            Long roleId = item.getRoleId();
            return roleId;
        }).collect(Collectors.toList());
        List<Role> roles = new ArrayList<>();
        for (Long roleId : roleIds) {
            LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Role::getId, roleId);
            Role role = this.getOne(wrapper);
            roles.add(role);
        }
        return roles;
    }
}
